package producer

import (
	"GraduationProject/public/models"
	"GraduationProject/public/tools"
	"encoding/json"
	"github.com/nsqio/go-nsq"
	"strconv"
)

var producer *nsq.Producer

func init() {
	var err error
	config := nsq.NewConfig()
	producer, err = nsq.NewProducer(tools.Conf.Get("nsqd_addr"), config)
	if err != nil {
		panic(err)
	}
}

func Product(r models.Record) error {
	bytes, err := json.Marshal(r)
	if err != nil {
		return err
	}
	err = producer.Publish(strconv.Itoa(int(r.SeedID)), bytes)
	if err != nil {
		return err
	}
	return nil
}
